package com.itheima.类加载与反射.反射.案例四;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class 暴力反射 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class ac = Class.forName("com.itheima.类加载与反射.反射.案例四.Student");
        //demo1(ac);
        //demo2(ac);
        //demo3(ac);
        //demo4(ac);
        //demo5(ac);
        //demo6(ac);


    }

    public static void demo6(Class ac) throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        Field money = ac.getDeclaredField("money");
        money.setAccessible(true);
        Student stu = (Student) ac.newInstance();
        Object o = money.get(stu);
        System.out.println(o);

        System.out.println(stu);
    }

    public static void demo5(Class ac) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
        Constructor con = ac.getDeclaredConstructor();
        Student stu= (Student) con.newInstance();
        Field money = ac.getDeclaredField("money");
        money.setAccessible(true);
        Object o = money.get(stu);
        System.out.println(o);
        money.set(stu,300);
        Object o1 = money.get(stu);
        System.out.println(o1);
    }

    public static void demo4(Class ac) throws NoSuchFieldException {
        Field number = ac.getField("number");
        System.out.println(number);
        Field money = ac.getField("money");
        System.out.println(money);
    }

    public static void demo3(Class ac) throws NoSuchFieldException {
        Field money = ac.getDeclaredField("money");
        System.out.println(money);
    }

    public static void demo2(Class ac) {
        Field[] fields = ac.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
    }

    public static void demo1(Class ac) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {

        Field[] decs = ac.getDeclaredFields();
        System.out.println(decs);
        System.out.println("===================");
        for (Field dec : decs) {
            System.out.println(dec);
        }
    }
}
